<template>
  <a-modal
    :title="title"
    :width="800"
    :ok="false"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :okButtonProps="{ props: { disabled: disableSubmit } }"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="model" :rules="validatorRules">
        <a-form-model-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="机构编码"
          prop="orgCode"
        >
          <a-input id="orgCode" placeholder="" v-model.trim="model.orgCode" />
        </a-form-model-item>
        <a-form-model-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="机构名称"
          prop="departName"
        >
          <a-input id="departName" placeholder="" v-model.trim="model.departName"/>
        </a-form-model-item>
        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="上级部门">
          <a-tree-select
            style="width: 100%"
            :dropdownStyle="{ maxHeight: '200px', overflow: 'auto' }"
            :treeData="departTree"
            v-model="model.parentId"
            placeholder=""
            :disabled="condition"
          >
          </a-tree-select>
        </a-form-model-item>
        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="机构类型" prop="orgCategory">
          <template v-if="defaultModel.orgCategory == 1">
            <a-radio-group name="orgCategory" v-model="model.orgCategory">
              <a-radio :value="2"> 分公司/代理商 <span style="color:red">(添加后不能修改,请确认)</span> </a-radio>
            </a-radio-group>
          </template>
          <template v-if="defaultModel.orgCategory == 2">
            <a-radio-group name="orgCategory" v-model="model.orgCategory">
              <a-radio :value="3"> 门店 <span style="color:red">(添加后不能修改,请确认)</span></a-radio>
            </a-radio-group>
          </template>
        </a-form-model-item>
        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="状态">
          <a-radio-group v-model="model.status">
              <a-radio value="1"> 启用 </a-radio>
              <a-radio value="0"> 禁用 </a-radio>
            </a-radio-group>
        </a-form-model-item>
        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="经营品牌" :required="true">
          <a-checkbox-group v-model="model.brandCodes">
            <a-checkbox v-for="(item,index) in brandList" :value="item.brandCode" :key="index" name="brandCodes">{{ item.brandName }}</a-checkbox>
          </a-checkbox-group>
        </a-form-model-item>
        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="排序">
          <a-input-number v-model="model.departOrder" />
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
import { httpAction,getAction } from '@/api/manage'
import { queryIdTree } from '@/api/api'
import pick from 'lodash.pick'
import ATextarea from 'ant-design-vue/es/input/TextArea'
export default {
  name: 'DepartModal1',
  components: { ATextarea },
  data() {
    return {
      departTree: [],
      orgTypeData: [],
      phoneWarning: '',
      departName: '',
      title: '操作',
      seen: false,
      visible: false,
      condition: true,
      disableSubmit: false,
      model: {},
      brandList:[],
      defaultModel: {
        departOrder: 0,
        orgCategory: '1',
      },
      menuhidden: false,
      menuusing: true,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },

      confirmLoading: false,
      validatorRules: {
        departName: [{ required: true, message: '必填项' }],
        orgCode: [{ required: true, message: '必填项' }],
        orgCategory: [{ required: true, message: '必填项' }],
        brandCodes: [{ required: true, message: '必填项' }],
      },
      url: {
        add: '/sys/sysDepart/addOrEdit1',
      },
      dictDisabled: true,
    }
  },
  created() {
    
  },
  methods: {
    loadTreeData() {
      var that = this
      queryIdTree().then((res) => {
        if (res.success) {
          that.departTree = []
          for (let i = 0; i < res.result.length; i++) {
            let temp = res.result[i]
            that.departTree.push(temp)
          }
        }
      })
    },
    loadBrandList() {
      this.brandList = []
      getAction('sys/merchant/posMerchantBussiness/pageList',{orgId: this.model.parentId ,bussType: 'BRAND'}).then((res) => {
        if (res.success) {
          for (let a of res.result.records) {
            if(a.bizStatus == 1){
              this.brandList.push({ brandName: a.bussName, brandCode: a.bussCode })
            }
          }
        }
      })
    },
    add(depart, orgCategory) {
      this.visible = true
      this.defaultModel.orgCategory = orgCategory
      this.model = Object.assign({status: '1'}, this.defaultModel, {orgCategory: parseInt(orgCategory) + 1})
      console.log(this.model)
      this.loadTreeData()
      this.model.parentId = depart != null ? depart.toString() : null

      if (depart) {
        this.dictDisabled = false
      } else {
        this.dictDisabled = true
      }

      this.loadBrandList()
    },
    edit(record) {
      this.visible = true
      this.model = Object.assign({}, this.defaultModel, record)
      this.loadTreeData()
      this.model.parentId = record != null ? record.toString() : null
    },
    close() {
      this.$emit('close')
      this.disableSubmit = false
      this.visible = false
      this.$refs.form.resetFields()
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate((valid) => {
        if (valid) {
          if(!that.model.brandCodes || that.model.brandCodes.length == 0){
            that.$message.warning('经营品牌必填')
            return false
          }

          that.confirmLoading = true
          httpAction(this.url.add, this.model, 'post')
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.loadTreeData()
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
            })
        } else {
          return false
        }
      })
    },
    handleCancel() {
      this.close()
    },
    validateMobile(rule, value, callback) {
      if (!value || new RegExp(/^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\d{8}$/).test(value)) {
        callback()
      } else {
        callback('您的手机号码格式不正确!')
      }
    },
  },
}
</script>

<style scoped>
</style>